package com.zidian.teacher.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import com.zidian.teacher.R;
import com.zidian.teacher.bean.EvaluateDetaiChartBean;
import com.zidian.teacher.view.ScatterView;
import com.zidian.teacher.view.StatisticsBean;
import com.zidian.teacher.view.StatisticsList;

/**
 * Created by Administrator on 2016/9/6 0006.
 */
public class EvaluateChartAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_TOP = 1;
    private Context context;
    private List<EvaluateDetaiChartBean> data;
    private StatisticsList list = new StatisticsList();


    public EvaluateChartAdapter(Context context, List<EvaluateDetaiChartBean> data) {
        this.context = context;
        this.data = data;

    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP;
        } else {
            return TYPE_ITEM;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        if (viewType == TYPE_ITEM) {

            View view = LayoutInflater.from(context).inflate(R.layout.item_evaluate_detail_chart, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_TOP) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_evaluate_detai_header, parent,
                    false);
            return new HeaderViewHolder(view);
        }
        return null;

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof ItemViewHolder) {


            ((ItemViewHolder) holder).scored.setText(data.get(position - 1).getScored() + "");
            ((ItemViewHolder) holder).type.setText(data.get(position - 1).getEvaluateType());
            ((ItemViewHolder) holder).tag.setText(data.get(position - 1).getEvaluateTag());
            ((ItemViewHolder) holder).color.setBackgroundColor(data.get(position - 1).getPonitColor());

            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, position);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemLongClick(holder.itemView, position);
                        return false;
                    }
                });
            }


        } else if (holder instanceof HeaderViewHolder) {

            List<StatisticsBean> mlist = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                StatisticsBean bean = new StatisticsBean(data.get(i).getPonitColor(), data.get(i).getScored());
                mlist.add(bean);

            }

            list.setList(mlist);
            list.setCount(mlist.size());
            ((HeaderViewHolder) holder).scatterView.setData(list);

            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, position);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemLongClick(holder.itemView, position);
                        return false;
                    }
                });
            }

        }

    }

    @Override
    public int getItemCount() {
        return data.size() == 0 ? 1 : data.size() + 1;
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {

        View color;
        TextView type;
        TextView tag;
        TextView scored;

        public ItemViewHolder(View view) {
            super(view);
            color = view.findViewById(R.id.evaluate_color);
            type = (TextView) view.findViewById(R.id.evaluate_type);
            tag = (TextView) view.findViewById(R.id.evaluate_tag);
            scored = (TextView) view.findViewById(R.id.evaluate_scored);
        }
    }



    static class HeaderViewHolder extends RecyclerView.ViewHolder {

        ScatterView scatterView;

        public HeaderViewHolder(View view) {
            super(view);
            scatterView = (ScatterView) view.findViewById(R.id.scatterView);
        }
    }


}
